package com.example.demo.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.demo.aop.Idempotents;
import com.example.demo.entity.CourseType;
import com.example.demo.entity.CourseTypeCondition;
import com.example.demo.entity.Message;
import com.example.demo.service.CourseTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.relational.core.sql.In;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping("/courseType")
@RestController
public class CourseTypeController {

    @Autowired
    private CourseTypeService courseTypeService;

    @RequestMapping("/getAll")
    public void getAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<CourseType> list =  courseTypeService.getAllCourseType();
        response.getWriter().write(JSONObject.toJSONString(Message.success(list)));
    }

    @RequestMapping("/getByCondition")
    public void getByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college = request.getParameter("college");
        String typeName = request.getParameter("typeName");
        List<CourseType> list =  courseTypeService.getCourseTypeByCondition(new CourseTypeCondition(college, typeName));
        response.getWriter().write(JSONObject.toJSONString(Message.success(list)));
    }

    @RequestMapping("/add")
    @Idempotents
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String college = request.getParameter("college");
        Integer typeFlag = Integer.valueOf(request.getParameter("typeFlag"));
        String typeName = request.getParameter("typeName");
        CourseType courseType = new CourseType();
        courseType.setTypeFlag(typeFlag);
        courseType.setCollege(college);
        courseType.setTypeName(typeName);
        courseTypeService.addCourseType(courseType);
        response.getWriter().write(JSONObject.toJSONString(Message.success("Success add")));
    }

    @RequestMapping("/update")
    public void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String college = request.getParameter("college");
        Integer typeFlag = Integer.valueOf(request.getParameter("typeFlag"));
        String typeName = request.getParameter("typeName");
        CourseType courseType = new CourseType();
        courseType.setId(id);
        courseType.setTypeFlag(typeFlag);
        courseType.setCollege(college);
        courseType.setTypeName(typeName);
        courseTypeService.updateCourseType(courseType);
        response.getWriter().write(JSONObject.toJSONString(Message.success("Success add")));
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        courseTypeService.deleteCourseType(id);
        response.getWriter().write(JSONObject.toJSONString(Message.success("Success add")));
    }
}
